package com.dominos.voldemortstress;

import java.io.IOException;

import org.junit.Test;

import voldemort.client.ClientConfig;

import static org.junit.Assert.*;

public class VoldemortStressTestNonMocked {

	@Test
	public void testSetClientConfig()
	{
		String bootstrapUrl = "tcp://localhost:6666";
		ClientConfig clientConfig = new ClientConfig();
		clientConfig.setBootstrapUrls(bootstrapUrl);
		
		VoldemortStress voldemortStress = new VoldemortStress();
		voldemortStress.setClientConfig(clientConfig);
		
		assertSame(clientConfig, voldemortStress.getClientConfig());
	}
	
	@Test
	public void testSetStressParameters()
	{
		StressParameters stressParameters = new StressParameters();
		stressParameters.setNumberofWriteThreads(100);
		stressParameters.setSizeOfWriteData(25000);
		
		VoldemortStress voldemortStress = new VoldemortStress();
		voldemortStress.setStressParameters(stressParameters);
		
		assertSame(stressParameters, voldemortStress.getStressParameters());
	}
	
	@Test
	public void testConnectToStore()
	{
		String bootstrapUrl = "tcp://localhost:6666";
		ClientConfig clientConfig = new ClientConfig();
		clientConfig.setBootstrapUrls(bootstrapUrl);
		

		StressParameters stressParameters = new StressParameters();
		stressParameters.setNumberofWriteThreads(100);
		stressParameters.setSizeOfWriteData(25000);	
		stressParameters.setStoreName("stressTestStore");
		
		VoldemortStress voldemortStress = new VoldemortStress();
		voldemortStress.setClientConfig(clientConfig);
		voldemortStress.setStressParameters(stressParameters);
		
		voldemortStress.connectToStore();
		
		assertTrue(voldemortStress.getStoreClient()!=null);
	}

	
	@Test
	public void testDisconnectFromStore()
	{
		String bootstrapUrl = "tcp://localhost:6666";
		ClientConfig clientConfig = new ClientConfig();
		clientConfig.setBootstrapUrls(bootstrapUrl);
		

		StressParameters stressParameters = new StressParameters();
		stressParameters.setNumberofWriteThreads(100);
		stressParameters.setSizeOfWriteData(25000);	
		stressParameters.setStoreName("stressTestStore");
		
		VoldemortStress voldemortStress = new VoldemortStress();
		voldemortStress.setClientConfig(clientConfig);
		voldemortStress.setStressParameters(stressParameters);
		
		voldemortStress.connectToStore();
		assertTrue(voldemortStress.getStoreClient()!=null);
		
		voldemortStress.disconnectFromStore();
		
		assertTrue(voldemortStress.getStoreClient()==null);
 	}

	@Test
	public void testStartStressTest()
	{
		String bootstrapUrl = "tcp://localhost:6666";
		ClientConfig clientConfig = new ClientConfig();
		clientConfig.setBootstrapUrls(bootstrapUrl);
		

		StressParameters stressParameters = new StressParameters();
		stressParameters.setNumberofWriteThreads(100);
		stressParameters.setSizeOfWriteData(25000);	
		stressParameters.setStoreName("stressTestStore");
		
		VoldemortStress voldemortStress = new VoldemortStress();
		voldemortStress.setClientConfig(clientConfig);
		voldemortStress.setStressParameters(stressParameters);
		
		voldemortStress.connectToStore();
		assertTrue(voldemortStress.getStoreClient()!=null);
		
		voldemortStress.startStressTest();
		assertEquals(stressParameters.getNumberofWriteThreads(),voldemortStress.getNumberOfActiveWriteThreads());
		
	}
	
	@Test
	public void testStopStessTest() throws InterruptedException, IOException
	{
		String bootstrapUrl = "tcp://localhost:6666";
		ClientConfig clientConfig = new ClientConfig();
		clientConfig.setBootstrapUrls(bootstrapUrl);
		

		StressParameters stressParameters = new StressParameters();
		stressParameters.setNumberofWriteThreads(100);
		stressParameters.setSizeOfWriteData(25000);	
		stressParameters.setStoreName("stressTestStore");
		
		VoldemortStress voldemortStress = new VoldemortStress();
		voldemortStress.setClientConfig(clientConfig);
		voldemortStress.setStressParameters(stressParameters);
		
		voldemortStress.connectToStore();
		assertTrue(voldemortStress.getStoreClient()!=null);
		
		voldemortStress.startStressTest();
		assertEquals(stressParameters.getNumberofWriteThreads(),voldemortStress.getNumberOfActiveWriteThreads());
		
		voldemortStress.stopStressTest();
		assertEquals(0, voldemortStress.getNumberOfActiveWriteThreads());
	}
	
	@Test
	public void testGetStatistics() throws InterruptedException, IOException
	{
		String bootstrapUrl = "tcp://localhost:6666";
		ClientConfig clientConfig = new ClientConfig();
		clientConfig.setBootstrapUrls(bootstrapUrl);
		

		StressParameters stressParameters = new StressParameters();
		stressParameters.setNumberofWriteThreads(100);
		stressParameters.setSizeOfWriteData(25000);	
		stressParameters.setStoreName("stressTestStore");
		
		VoldemortStress voldemortStress = new VoldemortStress();
		voldemortStress.setClientConfig(clientConfig);
		voldemortStress.setStressParameters(stressParameters);
		
		voldemortStress.connectToStore();
		assertTrue(voldemortStress.getStoreClient()!=null);
		
		voldemortStress.startStressTest();
		assertEquals(stressParameters.getNumberofWriteThreads(),voldemortStress.getNumberOfActiveWriteThreads());
		
		Thread.sleep(1000);
		voldemortStress.stopStressTest();
		assertEquals(0, voldemortStress.getNumberOfActiveWriteThreads());

		
		//Get average write time
		System.out.println("Average Writetime " + voldemortStress.getAverageWriteTime() + " ms");
		
		//Get Total Number of Writes
		System.out.println("Total Number of Writes " + voldemortStress.getTotalNumberOfWrites());
		
		//Get Write Errors
		System.out.println("Total Number of Write Errors " + voldemortStress.getTotalNumberOfWriteErrors());
		
		
	}
	
	@Test
	public void testAddATimeConstrainedStopAndStart() throws InterruptedException, IOException
	{
		String bootstrapUrl = "tcp://localhost:6666";
		ClientConfig clientConfig = new ClientConfig();
		clientConfig.setBootstrapUrls(bootstrapUrl);
		

		StressParameters stressParameters = new StressParameters();
		stressParameters.setNumberofWriteThreads(100);
		stressParameters.setSizeOfWriteData(25000);	
		stressParameters.setStoreName("stressTestStore");
		
		VoldemortStress voldemortStress = new VoldemortStress();
		voldemortStress.setClientConfig(clientConfig);
		voldemortStress.setStressParameters(stressParameters);
		
		voldemortStress.connectToStore();
		assertTrue(voldemortStress.getStoreClient()!=null);
		
		voldemortStress.runStressTestForXSeconds(1);

		
		//Get average write time
		System.out.println("Average Writetime " + voldemortStress.getAverageWriteTime() + " ms");
		
		//Get Total Number of Writes
		System.out.println("Total Number of Writes " + voldemortStress.getTotalNumberOfWrites());
		
		//Get Write Errors
		System.out.println("Total Number of Write Errors " + voldemortStress.getTotalNumberOfWriteErrors());
			
	}
	
	@Test
	public void clearStore()
	{
		String bootstrapUrl = "tcp://localhost:6666";
		ClientConfig clientConfig = new ClientConfig();
		clientConfig.setBootstrapUrls(bootstrapUrl);
		

		StressParameters stressParameters = new StressParameters();
		stressParameters.setNumberofWriteThreads(100);
		stressParameters.setSizeOfWriteData(25000);	
		stressParameters.setStoreName("stressTestStore");
		
		VoldemortStress voldemortStress = new VoldemortStress();
		voldemortStress.setClientConfig(clientConfig);
		voldemortStress.setStressParameters(stressParameters);
		
		voldemortStress.connectToStore();
		assertTrue(voldemortStress.getStoreClient()!=null);
		
		voldemortStress.clearStore();
	}
	
	@Test
	public void testForReadStress() throws InterruptedException, IOException
	{
		String bootstrapUrl = "tcp://localhost:6666";
		ClientConfig clientConfig = new ClientConfig();
		clientConfig.setBootstrapUrls(bootstrapUrl);
		
		StressParameters stressParameters = new StressParameters();
		stressParameters.setNumberofReadThreads(100);
		stressParameters.setStoreName("stressTestStore");
		
		VoldemortStress voldemortStress = new VoldemortStress();
		voldemortStress.setClientConfig(clientConfig);
		voldemortStress.setStressParameters(stressParameters);
		
		voldemortStress.connectToStore();
		assertTrue(voldemortStress.getStoreClient()!=null);
		
		voldemortStress.runStressTestForXSeconds(1);

		//Get average write time
		System.out.println("Average Readtime " + voldemortStress.getAverageReadTime() + " ms");
		
		//Get Total Number of Writes
		System.out.println("Total Number of Reads " + voldemortStress.getTotalNumberOfReads());
		
		//Get Write Errors
		System.out.println("Total Number of Read Errors " + voldemortStress.getTotalNumberOfReadErrors());
			
	}
	
	@Test
	public void testModifyMaxConnectionsPerNode() throws InterruptedException, IOException
	{
		String bootstrapUrl = "tcp://localhost:6666";
		ClientConfig clientConfig = new ClientConfig();
		clientConfig.setBootstrapUrls(bootstrapUrl);
		
		StressParameters stressParameters = new StressParameters();
		stressParameters.setNumberofReadThreads(100);
		stressParameters.setStoreName("stressTestStore");
		
		VoldemortStress voldemortStress = new VoldemortStress();
		voldemortStress.setClientConfig(clientConfig);
		voldemortStress.setStressParameters(stressParameters);
		
		voldemortStress.connectToStore();
		assertTrue(voldemortStress.getStoreClient()!=null);
		
		voldemortStress.runStressTestForXSeconds(5);

		//Get average write time
		System.out.println("Average Readtime " + voldemortStress.getAverageReadTime() + " ms");
		
		//Get Total Number of Writes
		System.out.println("Total Number of Reads " + voldemortStress.getTotalNumberOfReads());
		
		//Get Write Errors
		System.out.println("Total Number of Read Errors " + voldemortStress.getTotalNumberOfReadErrors());
			
		voldemortStress.disconnectFromStore();
		
		voldemortStress.setClientConfigMaxConnectionsPerNode(20);
		
		voldemortStress.runStressTestForXSeconds(5);

		//Get average write time
		System.out.println("Average Readtime " + voldemortStress.getAverageReadTime() + " ms");
		
		//Get Total Number of Writes
		System.out.println("Total Number of Reads " + voldemortStress.getTotalNumberOfReads());
		
		//Get Write Errors
		System.out.println("Total Number of Read Errors " + voldemortStress.getTotalNumberOfReadErrors());
		
		voldemortStress.disconnectFromStore();
		
		voldemortStress.setClientConfigMaxConnectionsPerNode(6);
		
		voldemortStress.runStressTestForXSeconds(5);

		//Get average write time
		System.out.println("Average Readtime " + voldemortStress.getAverageReadTime() + " ms");
		
		//Get Total Number of Writes
		System.out.println("Total Number of Reads " + voldemortStress.getTotalNumberOfReads());
		
		//Get Write Errors
		System.out.println("Total Number of Read Errors " + voldemortStress.getTotalNumberOfReadErrors());
	}
	
	
}
